Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Cannot read properties of undefined (reading > 'map')

I'm fetching data from an API and want to show them as texts, the title and desc worked fine but the categories didn't, the error I get is:

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

The Code:

export default function SinglePost() {

      const [post, setPost] = useState({});
      const [title, setTitle] = useState("");
      const [desc, setDesc] = useState("");
      const [categories, setCategories] = useState("");
      
        useEffect(() => {
        const getPost = async () => {
          const res = await axios.get("/posts/" + path);
          setPost(res.data);
          setTitle(res.data.title);
          setDesc(res.data.desc);
          setCategories(res.data.categories);
        };
        getPost()
      }, [path]);
      
      <div className="singlePost">
                <div className="singlePostInfo">
               <div className="postCats">
              {post.categories.map((c) => (
                    <span className="postCat" key={c._id}>
                      Category: {c.name}
                      </span>
                  ))}
              </div> 
      </div>
    }

I don't know where my mistake is!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you need to update your render method to render categories from the state and the default value of categories state should be [] instead of ""

Change const [categories, setCategories] = useState("") to const [categories, setCategories] = useState([])

Change post.categories.map to categories.map and it should work!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!